/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.clientpackets;

import java.util.Calendar;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.Config;
import l1j.server.echo.ClientThread;
import l1j.server.server.datatables.AuctionBoardTable;
import l1j.server.server.datatables.CastleTable;
import l1j.server.server.datatables.HouseTable;
import l1j.server.server.datatables.ItemTable;
import l1j.server.server.datatables.NpcActionTable;
import l1j.server.server.datatables.lock.CharItemsReading;
import l1j.server.server.model.L1Clan;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.item.L1ItemId;
import l1j.server.server.model.npc.L1NpcHtml;
import l1j.server.server.model.npc.action.L1NpcAction;
import l1j.server.server.serverpackets.S_NPCTalkReturn;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.templates.L1AuctionBoard;
import l1j.server.server.templates.L1Castle;
import l1j.server.server.templates.L1House;

// Referenced classes of package l1j.server.server.clientpackets:
// ClientBasePacket, C_Amount

public class C_Amount extends ClientBasePacket {

	private static final String C_AMOUNT = "[C] C_Amount";
	private static final Logger _log = Logger.getLogger(C_Amount.class.getName());
	@Override
	public String getType() {
		return C_AMOUNT;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);
			final int objectId = this.readD();
			int amount = this.readD();
			this.readC();
			final String s = this.readS();
			if(s == null){
				return;
			}
			final L1PcInstance pc = client.getActiveChar();
			final L1NpcInstance npc = (L1NpcInstance) L1World.getInstance().findObject(objectId);
			if (npc == null) {
				return;
			}

			String s1 = "";
			String s2 = "";
			try {
				final StringTokenizer stringtokenizer = new StringTokenizer(s);
				s1 = stringtokenizer.nextToken();
				s2 = stringtokenizer.nextToken();
			} catch (final NoSuchElementException e) {
				s1 = "";
				s2 = "";
			}
			if (s1.equalsIgnoreCase("agapply")) { // 競売に入札した場合
				final String pcName = pc.getName();
				final AuctionBoardTable boardTable = new AuctionBoardTable();
				for (final L1AuctionBoard board : boardTable.getAuctionBoardTableList()) {
					if (pcName.equalsIgnoreCase(board.getBidder())) {
						pc.sendPackets(new S_ServerMessage(523)); // すでに他の家の競売に参加しています。
						return;
					}
				}
				final int houseId = Integer.valueOf(s2);
				final L1AuctionBoard board = boardTable.getAuctionBoardTable(houseId);
				if (board != null) {
					final int nowPrice = board.getPrice();
					final int nowBidderId = board.getBidderId();
					if (pc.getInventory().consumeItem(L1ItemId.ADENA, amount)) {
						// 競売掲示板を更新
						board.setPrice(amount);
						board.setBidder(pcName);
						board.setBidderId(pc.getId());
						boardTable.updateAuctionBoard(board);
						if (nowBidderId != 0) {
							// 入札者にアデナを返金
							final L1PcInstance bidPc = (L1PcInstance) L1World
									.getInstance().findObject(nowBidderId);
							if (bidPc != null) { // オンライン中
								bidPc.getInventory().storeItem(L1ItemId.ADENA,
										nowPrice);
								// あなたが提示された金額よりももっと高い金額を提示した方が現れたため、残念ながら入札に失敗しました。%n
								// あなたが競売に預けた%0アデナをお返しします。%nありがとうございました。%n%n
								bidPc.sendPackets(new S_ServerMessage(525, String
										.valueOf(nowPrice)));
							} else { // オフライン中
								final L1ItemInstance item = ItemTable.getInstance().createItem(L1ItemId.ADENA);
								item.setCount(nowPrice);
								CharItemsReading.get().storeItem(nowBidderId, item);
							}
						}
					} else {
						pc.sendPackets(new S_ServerMessage(189)); // \f1アデナが不足しています。
					}
				}
			} else if (s1.equalsIgnoreCase("agsell")) { // 家を売った場合
				final int houseId = Integer.valueOf(s2);
				final AuctionBoardTable boardTable = new AuctionBoardTable();
				final L1AuctionBoard board = new L1AuctionBoard();
				if (board != null) {
					// 競売掲示板に新規書き込み
					board.setHouseId(houseId);
					final L1House house = HouseTable.getInstance().getHouseTable(houseId);
					board.setHouseName(house.getHouseName());
					board.setHouseArea(house.getHouseArea());
					final TimeZone tz = TimeZone.getTimeZone(Config.TIME_ZONE);
					final Calendar cal = Calendar.getInstance(tz);
					cal.add(Calendar.DATE, 5); // 5日後
					cal.set(Calendar.MINUTE, 0); // 分、秒は切り捨て
					cal.set(Calendar.SECOND, 0);
					board.setDeadline(cal);
					board.setPrice(amount);
					board.setLocation(house.getLocation());
					board.setOldOwner(pc.getName());
					board.setOldOwnerId(pc.getId());
					board.setBidder("");
					board.setBidderId(0);
					boardTable.insertAuctionBoard(board);

					house.setOnSale(true); // 競売中に設定
					house.setPurchaseBasement(true); // 地下アジト未購入に設定
					HouseTable.getInstance().updateHouse(house); // DBに書き込み
				}
				/************************opqlo 城堡升級系統 *******************************/
			} else if(s.equalsIgnoreCase("貢獻經驗值")){
				final L1ItemInstance item = pc.getInventory().findItemId(780044);
				if(item!=null){
					if(amount > item.getCount()){
						amount = (int) item.getCount();
					}
					pc.getInventory().removeItem(item , amount);
					final L1Clan clan = pc.getClan();
					final L1Castle castle = CastleTable.getInstance().getCastleTable(clan.getCastleId());
					final byte CastleLv = castle.getCastleLv();
					castle.addCastleExp(amount);
					final String pcname = pc.getName();
					final boolean LvUp = castle.getCastleLv() !=CastleLv;
					for(final L1PcInstance clanPc : clan.getOnlineClanMember()){
						clanPc.sendPackets(new S_SystemMessage("\\aH成員:["+pcname+"]向城堡繳交( "+amount+" )貢獻石。"));
						if(LvUp){
							clanPc.sendPackets(new S_SystemMessage("\\aH城堡獲得貢獻值後升級為"+castle.getCastleLv()+"級。"));
						}
					}
					final String[] value = {castle.getName() , String.valueOf(castle.getCastleLv()) , String.valueOf(castle.getCastleExp()),String.valueOf(castle.getNextLvNeedExp()),String.valueOf(castle.getSkillPoint())};

					pc.sendPackets(new S_NPCTalkReturn(objectId, "CastleHa1",value));
				}
				/************************opqlo 城堡升級系統 *******************************/

			} else {
				final L1NpcAction action = NpcActionTable.getInstance().get(s, pc, npc);
				if (action != null) {
					final L1NpcHtml result = action.executeWithAmount(s, pc, npc, amount);
					if (result != null) {
						pc.sendPackets(new S_NPCTalkReturn(npc.getId(), result));
					}
					return;
				}
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}
}
